package com.lxxie.bean;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.List;

/**
 * Created by xielx on 20/11/25 17:05
 */
public class LayUIResponse implements Serializable {
        private int code;
        private String msg;
        private int count;
        private Object data;

        public static LayUIResponse success(Object data){
                LayUIResponse response = new LayUIResponse();
                response.setCode(0);
                response.setMsg("执行成功");
                response.setData(data);
                return response;
        }

        public static <T>LayUIResponse success(List<T> data){
                LayUIResponse response = new LayUIResponse();
                response.setCode(0);
                response.setMsg("执行成功");
                response.setData(data);
                response.setCount(data.size());
                return response;
        }

        public static LayUIResponse fail(String msg) {
                LayUIResponse response = new LayUIResponse();
                response.setCode(-1);
                response.setMsg(msg);
                return response;
        }

        public static LayUIResponse fail(Exception e) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                LayUIResponse response = new LayUIResponse();
                response.setCode(-1);
                response.setMsg(sw.toString());
                return response;
        }

        public int getCode() {
                return code;
        }

        public void setCode(int code) {
                this.code = code;
        }

        public String getMsg() {
                return msg;
        }

        public void setMsg(String msg) {
                this.msg = msg;
        }

        public int getCount() {
                return count;
        }

        public void setCount(int count) {
                this.count = count;
        }

        public Object getData() {
                return data;
        }

        public void setData(Object data) {
                this.data = data;
        }
}
